package com.base.controller;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.base.po.voip.Abouts;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.AboutsService;
import com.base.service.UsersService;

/**
 * 
 * 联系我们客户端
 * 
 * @author f
 * 
 */
@Controller
@RequestMapping("/admin")
public class wap_about {

	@Autowired
	private AboutsService aboutsService; // 联系信息
	@Autowired
	private UsersService usersService; // 代理商信息
	@Autowired
	private SubscribersService subscribersService; // 用户信息

	/**
	 * App端进入联系我们信息
	 * 
	 * @param userid   用户ID
	 * @param parentid 应用ID
	 */
	@RequestMapping(value = "/aboutApp")
	public ModelAndView aboutApp(String userid, String parentid) {
		Subscribers sub = subscribersService.selectByPrimaryKey(userid); // 当前用户的登录信息
		// 用户已登录
		if (sub != null)
			parentid = sub.getShopid(); // 赋值用户的商家信息
		Abouts abouts = aboutsService.selectByUserID(parentid); // 商家的联系信息/代理商的联系信息
		// 用户已登录 并且用户商家ID不等于代理商ID 并且商家没有联系信息
		if (sub != null && parentid != sub.getParentid() && abouts == null) {
			parentid = sub.getParentid(); // 赋值用户的代理商信息
			abouts = aboutsService.selectByUserID(parentid); // 用户的代理商联系信息
		}
		// 没有代理商的联系信息
		if (abouts == null) {
			Users us = usersService.selectByPrimaryKey(parentid); // 代理商的信息
			// 代理商信息不为空
			if (us != null)
				abouts = aboutsService.selectByUserID(us.getParentid()); // 获取代理商的上级联系信息
			// 代理商信息不为空 并且上级代理商不是ADMIN 并且上级联系为空
			if (us != null && !us.getParentid().equals("7dd9a07e-9cef-4702-a165-b7033861c1b7") && abouts == null)
				abouts = aboutsService.selectByUserID("7dd9a07e-9cef-4702-a165-b7033861c1b7"); // 获取ADMIN的联系信息
		}
		// ADMIN没有联系方式 为空
		if (abouts == null)
			abouts = new Abouts(null, null, "未设置", "未设置", "未设置", null, null, true);
		return new ModelAndView("jsp/wap_about", "abouts", abouts);
	}

	// App端进入公众号图片
	@RequestMapping(value = "/pic")
	public ModelAndView pic(String pic) {
		return new ModelAndView("jsp/wap_about_wechat", "pic", pic);
	}
}
